package handlers

import (
	"luntan/pkg/database"
	"luntan/pkg/models"
	"net/http"
)

type UserMessage struct {
	Status int `json:"status"`
	// say something to notfy
	Msg string `json:"msg,omitempty"`
	// pass data to js
	Users []models.User `json:"users,omitempty"`
}

func User_manager(w http.ResponseWriter, req *http.Request) {
	msg := UserMessage{}
	rows, err := database.SelectUsers()
	if err != nil {
		msg.Status = -1
		msg.Msg = "select users wrong"
		UserResp(w, &msg)
		return
	}
	for rows.Next() {
		var user models.User
		err := rows.Scan(&user.Id, &user.Account, &user.Password, &user.Status)
		if err != nil {
			msg.Status = -1
			msg.Msg = "scan user wrong"
			UserResp(w, &msg)
			return
		}
		msg.Users = append(msg.Users, user)
	}
	UserResp(w, &msg)
}
